package com.hsck.order.kds.base.common

import android.util.SparseArray
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.hsck.order.kds.base.utils.LogUtil

/**
 * 创建者 : ly
 * 时间 : 2017/9/15
 * 公司 : 工博
 * 功能 :
 */
fun ViewGroup.inflate(layoutRes: Int): View {
    return LayoutInflater.from(context).inflate(layoutRes, this, false)
}

fun <T : View> View.findViewOften(viewId: Int): T {
    val viewHolder: SparseArray<View> = tag as? SparseArray<View> ?: SparseArray()
    tag = viewHolder
    var childView: View? = viewHolder.get(viewId)
    if (null == childView) {
        childView = findViewById(viewId)
        viewHolder.put(viewId, childView)
    }
    return childView as T
}

fun View.setOnSingleClickListener(interval: Long = 600, onClick: (View) -> Unit) {
    try {
        var lastClickTime = 0L
        setOnClickListener { view ->

            val currentTime = System.currentTimeMillis()
            if (currentTime - lastClickTime >= interval) {
                lastClickTime = currentTime
                onClick(view)
            }
        }
    } catch (e: Exception) {
        LogUtil.e("error" + e.message)
    }
}